class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        stack<int> s;
        int i = 0, n = popped.size();
        for (auto e : pushed)
        {
            s.push(e);
            while (s.size() && s.top() == popped[i])
            {
                s.pop();
                i++;
            }
        }
        return i == n;

    }
};